{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d48dae04",
   "metadata": {},
   "source": [
    "### 字典遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ce2e7307",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_01 = {\n",
    "    \"stu2001\" : \"xiaohong\",\n",
    "    \"stu2002\" : \"xiaoming\",\n",
    "    \"stu2003\" : \"xiaoli\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f42d6e9d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2001': 'xiaohong', 'stu2002': 'xiaoming', 'stu2003': 'xiaoli'}\n"
     ]
    }
   ],
   "source": [
    "print(stu_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5ea88f8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu2001\n",
      "stu2002\n",
      "stu2003\n"
     ]
    }
   ],
   "source": [
    "# 遍历一个字典\n",
    "# i 是 item 在一个字典中是什么？\n",
    "\n",
    "for i in stu_01:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fe4899f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu2001 xiaohong\n",
      "stu2002 xiaoming\n",
      "stu2003 xiaoli\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历整个字典的 items\n",
    "# 对许多相同的代码进行同样的操作\n",
    "\n",
    "for k,v in stu_01.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1c7933fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2001': 'xiaohong', '2002': 'xiaoming', '2003': 'xiaoli'}\n"
     ]
    }
   ],
   "source": [
    "# 实践1：stu2001 中 取出数字，作为新的字典 key\n",
    "stu_02 = {}\n",
    "for k,v in stu_01.items():\n",
    "    stu_02[k[3:]] = v  # 用字典新增的方式创建字典\n",
    "print(stu_02)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ec51a0f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2001': 'XIAOHONG', '2002': 'XIAOMING', '2003': 'XIAOLI'}\n"
     ]
    }
   ],
   "source": [
    "# 实践2: value 我也想变， 所有字母大写\n",
    "stu_02 = {}\n",
    "for k,v in stu_01.items():\n",
    "    stu_02[k[3:]] = v.upper()\n",
    "print(stu_02)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4f3882ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2001: 'xiaohong', 2002: 'xiaoming', 2003: 'xiaoli'}\n"
     ]
    }
   ],
   "source": [
    "# 实践3：key 由 ‘str’变成 int\n",
    "stu_03 = {}\n",
    "for k,v in stu_01.items():\n",
    "    stu_03[int(k[3:])] = v\n",
    "print(stu_03)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "028a26e0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
